local dofile = dofile
local format = string.format

local FILES = {
	"global", "unit", "frame", "dock", "player", "cursor", "keys",
	"actions", "bindings", "inventory", "chat", "options", "cvar",
	"buff", "lang", "zone", "pvp", "party", "mirror", "spellbook",
	"guild", "lfm", "skill", "trade", "wstate", "addon", "channel",
	"loot", "macro", "talent", "gmticket", "stats", "quests", "auction",
	"trainer", "craft", "raid", "profile", "modclick", "tracking",
	"voice", "sound", "social", "locale", "spell", "pet", "totem",
	"combatlog", "refer", "vehicle", "companions", "rune", "achievement",
	"threat", "calendar", "guildbank", "currency", "barber", "securecmd",
	"mail", "joystick",
}

local GLOBALS = {
	Addons = true,
	bindings = true,
	modclick = true,
	UI = true,
	os = true,
	cvar = true,
	print = true,
	config = true,
	Security = true,
	debug = true,
	units = true,
	db = true,
}

local env = api.global
setmetatable(env, {
	__index = function(_, key)
		if GLOBALS[key] then
			return _G[key]
		end
	end
})

for _, name in ipairs(FILES) do
	local s = loadfile(format("api/%s.lua", name))
	if s then
		setfenv(s, env)
		s()
	else
		print("ERROR loading", name)
	end
end
setmetatable(env, nil)

dofile("protected.lua")
